package com.hsnn.medstgmini.trading.pur.dao.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.exception.MSException;
import com.hsnn.medstgmini.trading.pur.dao.PurOrderLogisticsDao;
import com.hsnn.medstgmini.trading.pur.model.PurOrderLogistics;
import com.hsnn.medstgmini.trading.pur.model.form.PurOrderLogisticsForm;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;
@Repository
public class PurOrderLogisticsDaoImpl implements PurOrderLogisticsDao {
	@Autowired
	private HibernateSqlUtil dao;
	
	/**
	 * 
	 *@category 根据物流信息ID获取物流信息
	 *@author 邱磊
	 *@date 2015年7月15日 下午4:24:56
	 *@param interflowId
	 *@return
	 */
	public PurOrderLogistics getLogistics(String interflowId){
		PurOrderLogistics data = (PurOrderLogistics) dao.getUniquerResult(PurOrderLogistics.class, interflowId);
		return data;
	}
	/**
	 * 
	 *@category 更新物流信息
	 *@author 邱磊
	 *@date 2015年7月15日 下午4:51:11
	 *@param form
	 */
	public Pagination updateLogistics(Pagination page){
		PurOrderLogisticsForm form = (PurOrderLogisticsForm) page.getForm();
		PurOrderLogistics data = (PurOrderLogistics) dao.getUniquerResult(PurOrderLogistics.class, form.getInterflowId());
		if (null != data){
			if (null != data.getRecpDatetime()){
				page.setSuccess(false);
				page.setMsg("该物流已送达，不可重复更新！");
				return page;
			}
			if (ParseDate.fromFullFormat(DateUtil.dateTime(data.getDelDatetime())).getTime() > ParseDate.fromFullFormat(form.getInterflowTime()).getTime()){
				page.setSuccess(false);
				page.setMsg("物流运送时间不能小于出库时间！");
				return page;
			}
			if (null != form.getRecpDatetime() && !"".equals(form.getRecpDatetime().trim())){
				if (ParseDate.fromFullFormat(DateUtil.dateTime(data.getDelDatetime())).getTime() > ParseDate.fromFullFormat(form.getRecpDatetime()).getTime()){
					page.setSuccess(false);
					page.setMsg("物流送达时间不能小于出库时间！");
					return page;
				}
			}
			
			data.setInterflowDetail(data.getInterflowDetail()+";"+form.getInterflowTime()+" "+form.getInterflowDetail());
			data.setRecpDatetime(form.getRecpDatetime()==null?null:form.getRecpDatetime().trim().equals("")?null:ParseDate.fromFullFormat(form.getRecpDatetime()));
			data.setUpdDatetime(new Date());
			data.setUpdUser(form.getUpdUser());
			try {
				dao.update(data);
				page.setSuccess(true);
			} catch (Exception e) {
				throw new MSException("物流信息更新失败"); 
			}
		} else {
			
			if (ParseDate.fromFullFormat(form.getDelDatetime()).getTime() > ParseDate.fromFullFormat(form.getInterflowTime()).getTime()){
				page.setSuccess(false);
				page.setMsg("物流运送时间不能小于出库时间！");
				return page;
			}
			if (null != form.getRecpDatetime() && !"".equals(form.getRecpDatetime().trim())){
				if (ParseDate.fromFullFormat(form.getDelDatetime()).getTime() > ParseDate.fromFullFormat(form.getRecpDatetime()).getTime()){
					page.setSuccess(false);
					page.setMsg("物流送达时间不能小于出库时间！");
					return page;
				}
			}
			PurOrderLogistics logistics = new PurOrderLogistics();
			logistics.setInterflowId(form.getInterflowId());
			logistics.setWithOrderId(form.getWithOrderId());
			logistics.setDelDatetime(ParseDate.fromFullFormat(form.getDelDatetime()));
			logistics.setInterflowDetail(form.getInterflowTime()+" "+form.getInterflowDetail());
			logistics.setRecpDatetime(form.getRecpDatetime()==null?null:form.getRecpDatetime().trim().equals("")?null:ParseDate.fromFullFormat(form.getRecpDatetime()));
			logistics.setUpdDatetime(new Date());
			logistics.setUpdUser(form.getUpdUser());
			try {
				dao.add(logistics);
				page.setSuccess(true);
			} catch (Exception e) {
				throw new MSException("物流信息更新失败"); 
			}
		}
		return page;
	}
}
